package CompletableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author chaird
 * @create 2022-03-30 10:05
 */
public class TeaForCompletableFuture {
  public static void main(String[] args) throws Exception{
      CompletableFuture<String> task1CompletableFuture = CompletableFuture.supplyAsync(() -> {
          System.out.println("洗水壶、烧开水开始（1+15分钟）-----");
          try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } finally { }
          System.out.println("洗水壶、烧开水结束（1+15分钟）-----");
          return "task 1 ok";
      });

      CompletableFuture<String> task2CompletableFuture = CompletableFuture.supplyAsync(() -> {
          System.out.println("洗茶壶、洗茶杯、拿茶叶开始（1+2 +1分钟）-----");
          try { TimeUnit.SECONDS.sleep(5); } catch (Exception e) { e.printStackTrace(); } finally { }
          System.out.println("洗茶壶、洗茶杯、拿茶叶结束（1+2 +1分钟）-----");
          return "task 2 ok";
      });

      //
      CompletableFuture<String> resFuture = task1CompletableFuture.thenCombine(task2CompletableFuture, (task1Res, task2Res) -> {
          System.out.println("主线程开始泡茶：" + task1Res + "---" + task2Res);
          System.out.println("主线程结束泡茶：" + task1Res + "---" + task2Res);
          return "泡茶成功";
      });

      String s = resFuture.get();
      System.out.println(s);
  }
}
